package com.alcloud.springplus.controller;

import com.alcloud.springplus.base.GenericService;
import com.alcloud.springplus.base.ResponseCode;
import com.alcloud.springplus.base.ResponseData;
import com.alcloud.springplus.constants.Constants;
import com.alcloud.springplus.util.ServletUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @className: GenericController
 * @description: 通用Controller
 * @author: AllG
 * @createDate: 2018/12/4 14:51
 * @updateRemark: 更新说明
 */
@CrossOrigin
public abstract class GenericController<T, ID> {

    @Autowired
    protected GenericService<T, ID> genericService;

    public GenericController(GenericService<T, ID> genericService) {
        this.genericService = genericService;
    }


    /**
     * @param id
     * @return java.lang.String
     * @method get
     * @description 通过Id查询实体
     * @date: 2018/12/4 14:55
     * @author: AllG
     */
    @GetMapping("/{id}")
    public ResponseData<T> getById(@PathVariable("id") ID id) {
        return new ResponseData<>(ResponseCode.SUCCESS,genericService.findById(id).orElse(null));
    }


    /**
     * @method  save
     * @description 新增保存数据
     * @date: 2018/12/5 11:13
     * @author: AllG
     * @param t
     * @return com.alcloud.springplus.base.ResponseData<T>
     */
    @PostMapping("/")
    public ResponseData<T> save(@RequestBody T t) {
        return new ResponseData<>(ResponseCode.SUCCESS,genericService.save(t));
    }

    /**
     * @method  updateById
     * @description 根据Id更新数据
     * @date: 2018/12/5 11:42
     * @author: AllG
     * @param id
     * @param t
     * @return com.alcloud.springplus.base.ResponseData<T>
     *
     */
    @PutMapping("/{id}")
    public ResponseData<T> updateById(@PathVariable("id") ID id,@RequestBody T t){
        Optional<T> findT= genericService.findById(id);
        if(findT.isPresent()){
            BeanUtils.copyProperties(t,findT.get(),"id");
        }else{
            return new ResponseData<>(ResponseCode.UPDATE_EXCEPTION_NOSOURCE);
        }

        return new ResponseData<>(ResponseCode.SUCCESS,genericService.save(findT.get()));
    }


    /**
     * @method  existsById
     * @description 通过Id判断是否存在
     * @date: 2018/12/5 11:50
     * @author: AllG
     * @param id
     * @return com.alcloud.springplus.base.ResponseData<T>
     */
    @GetMapping("/exists/{id}")
    public ResponseData<Boolean> existsById(@PathVariable("id") ID id){
        return new ResponseData<>(ResponseCode.SUCCESS,genericService.existsById(id));
    }

    /**
     * @method  deleteById
     * @description 通过Id删除内容
     * @date: 2018/12/5 11:59
     * @author: AllG
     * @param id
     * @return com.alcloud.springplus.base.ResponseData
     */
    @DeleteMapping("/{id}")
    public ResponseData deleteById(@PathVariable("id") ID id){
        genericService.deleteById(id);
        return new ResponseData<>(ResponseCode.SUCCESS);
    }

    // Iterable<T> findAllById(Iterable<ID> ids); TODO 后继处理

    // long count(); TODO 后继处理

    /**
     * @method  search
     * @description 通用分页查询
     * @date: 2018/12/6 11:12
     * @author: AllG
     * @param pageable
     * @param request
     * @return java.lang.String
     */
    @GetMapping("/search")
    public ResponseData<Page<T>> search(@PageableDefault(sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable, HttpServletRequest request){
        /**获取查询条件数据*/
        return new ResponseData<>(ResponseCode.SUCCESS,genericService.findAll(ServletUtils.getParamValuesMap(request, Constants.SEARCH_PREFIX),pageable));
    }

}
